package com.example.externalstoreage;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.room.Room;

import com.example.externalstoreage.dao.BookDao;
import com.example.externalstoreage.dao.UserDao;
import com.example.externalstoreage.database.BookDatabase;
import com.example.externalstoreage.entity.BookInfo;
import com.example.externalstoreage.entity.User;
import com.example.externalstoreage.utils.ToastHelper;

import java.util.List;

public class RoomExample extends AppCompatActivity implements View.OnClickListener {
    EditText etName;
    EditText etAuthor;
    EditText etPress;
    EditText etPrice;
    private BookDao bookDao;
    private EditText etId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_room_example);

        etName = findViewById(R.id.et_name);
        etAuthor = findViewById(R.id.et_author);
        etPress = findViewById(R.id.et_press);
        etPrice = findViewById(R.id.et_price);
        etId = findViewById(R.id.et_id);

        findViewById(R.id.btn_create).setOnClickListener(this);
        findViewById(R.id.btn_remove).setOnClickListener(this);
        findViewById(R.id.btn_edit).setOnClickListener(this);
        findViewById(R.id.btn_search).setOnClickListener(this);


        BookDatabase bookDatabase = MyApplication.getInstance().getBookDatabase(getApplicationContext());
        bookDao = bookDatabase.bookDao();
//        BookInfo book = new BookInfo();
//        book.setName("码农修仙记之漫漫练气期");
//        book.setAuthor("犬吠");
//        book.setPress("仙界大陆");
//        book.setPrice(9.9);
//        bookDao.insert(book);

        Log.d("zhoutao", "insert one to book_databse");

    }

    private BookInfo getBook() {
        String id = etId.getText().toString();
        String name = etName.getText().toString();
        String author = etAuthor.getText().toString();
        String press = etPress.getText().toString();
        String price = etPrice.getText().toString();


        BookInfo book = new BookInfo();
        if (id != null) {
            Log.d("zhoutao", "id: " + id);
            book.setId(Integer.parseInt(id));
        }
        if (name != null) {
            Log.d("zhoutao", "name: " + name);
            book.setName(name);
        }
        if (author != null) {
            Log.d("zhoutao", "author: " + author);
            book.setAuthor(author);
        }
        if (press != null) {
            Log.d("zhoutao", "press: " + press);
            book.setPress(press);
        }
        if (price != null) {
            Log.d("zhoutao", "price: " + price);
            book.setPrice(Double.parseDouble(price));
        }

        Log.d("zhoutao", book.toString());
        return book;
    }


    @Override
    public void onClick(View view) {


        int id = view.getId();

        if (R.id.btn_create == id) {
            // bookDao.insert(getBook());
            ToastHelper.show(this, "添加成功");
        } else if (R.id.btn_remove == id) {
            int bookId = Integer.parseInt(etId.getText().toString());
            BookInfo book = new BookInfo();
            book.setId(bookId);
            bookDao.delete(book);
        } else if (R.id.btn_edit == id) {
            getBook();
        } else if (R.id.btn_search == id) {
            List<BookInfo> list = bookDao.queryAll();
            for (BookInfo book : list) {
                Log.d("zhoutao", book.toString());
            }
        }
    }
}